Datos
Loading required package: lubridate
Attaching package: ‘lubridate’
The following objects are masked from ‘package:base’:
date, intersect, setdiff, union
Loading required package: PerformanceAnalytics
Loading required package: xts
Loading required package: zoo
Attaching package: ‘zoo’
The following objects are masked from ‘package:base’:
as.Date, as.Date.numeric
Attaching package: ‘PerformanceAnalytics’
The following object is masked from ‘package:graphics’:
legend
Loading required package: quantmod
Loading required package: TTR
Registered S3 method overwritten by 'quantmod':
method from
as.zoo.data.frame zoo
── Attaching core tidyverse packages ──────────────────────────────────────────────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.4 ✔ readr 2.1.5
✔ forcats 1.0.0 ✔ stringr 1.5.1
✔ ggplot2 3.4.4 ✔ tibble 3.2.1
✔ purrr 1.0.2 ✔ tidyr 1.3.0── Conflicts ────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::first() masks xts::first()
✖ dplyr::lag() masks stats::lag()
✖ dplyr::last() masks xts::last()
ℹ Use the ]8;;http://conflicted.r-lib.org/conflicted package]8;; to force all conflicts to become errors
Attaching package: ‘tsibble’
The following object is masked from ‘package:zoo’:
index
The following object is masked from ‘package:lubridate’:
interval
The following objects are masked from ‘package:base’:
intersect, setdiff, union
Loading required package: fabletools
Attaching package: ‘fable’
The following object is masked from ‘package:tidyquant’:
VAR
index = tq_index("SP500") %>% select(symbol, weight)
Getting holdings for SP500
index = index %>% filter(symbol != "-")
index
[1] 0.9961429
data = tq_get(index$symbol, get = "stock.prices", from = "2022-01-01", to = "2024-03-29") %>% select(symbol, date, adjusted)
Warning: There were 2 warnings in `dplyr::mutate()`.
The first warning was:
ℹ In argument: `data.. = purrr::map(...)`.
Caused by warning:
! x = 'GEV', get = 'stock.prices': Error in getSymbols.yahoo(Symbols = "GEV", env = <environment>, verbose = FALSE, : Unable to import “GEV”.
HTTP error 400.
Removing GEV.
ℹ Run ]8;;ide:run:dplyr::last_dplyr_warnings()dplyr::last_dplyr_warnings()]8;; to see the 1 remaining
warning.
symbols_from_data = unique(data$symbol)
data = pivot_wider(data, id_cols = date, names_from = symbol, values_from = adjusted)
data = mutate(data, t = row_number())
data = pivot_longer(data, all_of(symbols_from_data), names_to = "symbol", values_to = "adjusted")
data = as_tsibble(data, index = t, key = symbol)
Split de datos
train = data %>% filter_index(. ~ (562-30))
test = data %>% filter_index((562-30+1) ~ .)